#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "node.h"

Node* createNode(char* name, char* text){
    Node *pnode = (Node*)malloc(sizeof(Node));
    //pnode->name = name;
    //pnode->text = text;
    strcpy(pnode->name, name);
    strcpy(pnode->text, text);

    pnode->lineno = yylineno;
    pnode->parent =NULL;
    //pnode->child = (Node**)malloc(sizeof(Node*)*MAX_NUM);
    for(int i=0;i<MAX_CHILD_NUM;i++){
        pnode->child[i] = NULL;
    }
    pnode->childsum = 0;
    //pnode->child = NULL;
    //printNode(pnode, 0);
    //printMyNode(pnode);
    //printf("-------------%d----------\n\n", errorNum);
    //errorNum++;
    return pnode;
}


/*
void printNode(Node *pnode){
    if(strcmp(pnode->text, "")!=0){
        printf("    %s: %s\n", pnode->name, pnode->text);
    }else{
        printf("%s\n", pnode->name);

    }

}
*/

void addChild(int childsum, Node* parent, ...){
    va_list ap; 
    va_start(ap,parent);
    
    for(int i=0; i<childsum; i++){
        parent->child[i] = va_arg(ap, Node*);
        //parent->child[i]->parent = parent;

    }
    for(int i=0; i<childsum; i++){
    	parent->child[0]->parent = NULL;
    }
    parent->lineno = parent->child[0]->lineno;
    parent->childsum = childsum;
    va_end(ap);
}

void printTree(Node *parent, int blank){
    if(parent == NULL){
        return;
    }
    for(int i=0;i<blank;i++){
        printf(" ");
    }
    if(parent->childsum != 0){
        printf("%s (%d)\n", parent->name, parent->lineno);
        for(int i=0; i< parent->childsum; i++){
            printTree(parent->child[i],blank+2);
        }
    }else{
        if(strcmp(parent->name, "INT")==0){
            printf("%s: %d\n", parent->name, atoi(parent->text));
        }
        else if(strcmp(parent->name, "FLOAT")==0){
            printf("%s: %f\n", parent->name, atof(parent->text));
        }
        else if(strcmp(parent->name, "ID")==0 || strcmp(parent->name, "TYPE")==0){
            printf("%s: %s\n", parent->name, parent->text);
        }
        else{
            printf("%s\n", parent->name);
        }
    }

}